/**
 * 神州信息(2012)
 */
package net.abbp.report.model.cell.style;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import net.abbp.core.transform.xml.jaxb.ColorAdapter;
import net.abbp.core.util.BooleanUtils;
import net.abbp.core.util.NumberUtils;
import net.abbp.core.util.StringUtils;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;

/**
 * Jaxb Font适配器
 * 
 * @version 1.0
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ABBP 1.0
 * 
 *        Date：2012-4-17
 */
final public class FontStyleAdapter extends XmlAdapter<Object, FontStyle> {

    @Override
    public Object marshal(FontStyle fontStyle) throws Exception {
        try {
            if (fontStyle == null) {
                return null;
            }

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
            Element customXml = doc.createElement("fontStyle");

            customXml.setAttribute("Name", fontStyle.getName());
            customXml.setAttribute("Style", StringUtils.convertToString(fontStyle.getStyle(), ""));
            customXml.setAttribute("Size", StringUtils.convertToString(fontStyle.getSize(), ""));
            customXml.setAttribute("Foreground", ColorAdapter.toHexEncoding(fontStyle.getForeground()));
            customXml.setAttribute("Shadow", StringUtils.convertToString(fontStyle.isShadow(), "false"));
            customXml.setAttribute("Strikethrough", StringUtils.convertToString(fontStyle.isStrikethrough(), "false"));
            customXml.setAttribute("Subscript", StringUtils.convertToString(fontStyle.isSubscript(), "false"));
            customXml.setAttribute("Superscript", StringUtils.convertToString(fontStyle.isSuperscript(), "false"));
            customXml.setAttribute("Underline", StringUtils.convertToString(fontStyle.getUnderline(), "false"));

            return customXml;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public FontStyle unmarshal(Object domTree) throws Exception {
        Element content = (Element) domTree;
        NamedNodeMap nnm = content.getAttributes();

        FontStyle fontStyle = FontStyle.getInstance();

        fontStyle = fontStyle.applyName(nnm.getNamedItem("Name").getTextContent());
        fontStyle = fontStyle.applyStyle(NumberUtils.convertToInteger(nnm.getNamedItem("Style").getTextContent(), 0));
        fontStyle = fontStyle.applySize(NumberUtils.convertToInteger(nnm.getNamedItem("Size").getTextContent(), 12));
        fontStyle = fontStyle.applyForeground(ColorAdapter
                .parseToColor(nnm.getNamedItem("Foreground").getTextContent()));

        fontStyle = fontStyle.applyShadow(BooleanUtils.parseString(nnm.getNamedItem("Shadow").getTextContent()));
        fontStyle = fontStyle.applyStrikethrough(BooleanUtils.parseString(nnm.getNamedItem("Strikethrough")
                .getTextContent()));
        fontStyle = fontStyle.applySubscript(BooleanUtils.parseString(nnm.getNamedItem("Subscript").getTextContent()));
        fontStyle = fontStyle.applySuperscript(BooleanUtils.parseString(nnm.getNamedItem("Superscript")
                .getTextContent()));
        fontStyle = fontStyle.applyUnderline(NumberUtils.convertToInteger(nnm.getNamedItem("Underline")
                .getTextContent(), 0));

        return fontStyle;
    }
}
